package com.skyworth.utils;

import java.util.regex.Pattern;

/**
 * Created by skyworth on 2016/7/15.
 */
public class StrUtil {
	public static boolean isNumbericDigit(String str){
		if(str==null || str.length()==0){
			return false;
		}
		for(int i = str.length(); --i >= 0;){
			if(!Character.isDigit(str.charAt(i))){
				return false;
			}
		}
		return true;
	}

	public static boolean isNumbericPattern(String str){
		if(str==null || str.length()==0){
			return false;
		}
		Pattern pattern = Pattern.compile("[0-9]*");
		return pattern.matcher(str).matches();
	}

	public static boolean isNumbericAscii(String str){
		if(str==null || str.length()==0){
			return false;
		}
		for(int i=str.length(); --i>=0;){
			int chr = str.charAt(i);
			if(chr < 48 || chr>57){
				return false;
			}
		}
		return true;
	}

	public static boolean isIntegerPattern(String str) {
		if(str==null || str.length()==0){
			return false;
		}
		Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
		return pattern.matcher(str).matches();
	}

	public static int stringToInt(String str, int defaultVal) {
		int val;
		try {
			val = Integer.parseInt(str);
		} catch (Exception e) {
			val = defaultVal;
		}
		return val;
	}


	public static boolean isEmpty(String text){
		return text == null || text.trim().length() <= 0;
	}



	public static String objectToString(Object o){
		if(o!=null && !o.equals("")){
			return String.valueOf(o);
		}
		return "";
	}

}
